<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：CommonController.class.php
 * @类功能: 共通类
 * @开发者: zc
 * @开发时间： 15-04-02
 * @版本：version 1.0
 */
namespace app\agent\controller;

use app\common\cache\article\IArticleCache;
use app\common\controller\AgentBaseController;
use app\common\enum\ArticleStatus;
use app\common\ext\IDb;
use app\common\ext\IPhoto;
use app\common\ext\IRequest;

class Common extends AgentBaseController {

    /**
     * @功能：上传图片
     * @开发者：cxl
     */
    public function upload() {
        $type = IRequest::get('type', IRequest::NOT_EMPTY, '图片类型不能为空');
        $info = IPhoto::upload($type, "_upload_");
        echo json_encode($info);
    }

    /**
     * @功能：上传图片
     * @开发者：cxl
     */
    public function oss() {
        $type = IRequest::get('type', IRequest::NOT_EMPTY, '图片类型不能为空');
        $info = IPhoto::oss($type, "_upload_");
        echo json_encode($info);
    }

    /**
     * @功能：文本编辑器上传图片
     * @开发者：cxl
     */
    public function file() {
        $info = IPhoto::oss("article", "upfile");
        echo json_encode($info);
    }

    /**
     * @功能：采集文章接口
     * @开发者：cxl
     */
    public function article() {
        //接收参数
        $category_id = IRequest::get('category_id');
        $title = IRequest::get('title');
        $master_url = IRequest::get('master_url');
        $content = IRequest::get('content');
        $source = IRequest::get('source');
        $source_url = IRequest::get('source_url');

        //判断分类是否正确
        if(!in_array($category_id,array(1,2,3,4,5,6))){
            $response['code'] = "99";
            $response['message'] = "文章分类编号错误！";
            exit(json_encode($response));
        }

        //判断文章标题
        if(empty($title)){
            $response['code'] = "99";
            $response['message'] = "文章标题不能为空！";
            exit(json_encode($response));
        }

        //判断文章标题
        if(empty($content)){
            $response['code'] = "99";
            $response['message'] = "文章内容不能为空！";
            exit(json_encode($response));
        }else{
            $content = base64_decode($content);
            $content = ltrim($content,"\"");
            $content = rtrim($content,"\"");
        }

        //判断文章是否已经存在
        $where['source_url'] = $source_url;
        $article_count = IDb::getInstance("article")->setDbWhere($where)->con();
        if($article_count <= 0){
            //整理数据
            $data['category_id'] = $category_id;
            $data['title'] = $title;
            $data['content'] = $content;
            $data['master_url'] = $master_url;
            $data['source'] = $source;
            $data['browse'] = rand(111,999999);
            $data['article_status'] = ArticleStatus::Release;
            $data['source_url'] = $source_url;
            $data['create_time'] = get_date_time();
            $article_add = IDb::getInstance("article")->setDbData($data)->add();
            if($article_add === false){
                $response['code'] = "99";
                $response['message'] = "添加文章资讯失败！";
                exit(json_encode($response));
            }
        }else{
            $response['code'] = "99";
            $response['message'] = "重复数据！";
            exit(json_encode($response));
        }

        //清除缓存
        IArticleCache::deleteList();
        IArticleCache::deleteSearchList();

        //添加成功
        $response['code'] = "0";
        $response['message'] = "添加成功！";
        exit(json_encode($response));
    }

    /**
     * @功能：采集问答接口
     * @开发者：cxl
     */
    public function question() {
        //接收参数
        $question_type = IRequest::get('question_type');
        $question_content = IRequest::get('question_content');
        $reply_content = IRequest::get('reply_content');
        $source_url = IRequest::get('source_url');

        //判断分类是否正确
        if(!in_array($question_type,array(1,2,3))){
            $response['code'] = "99";
            $response['message'] = "问答分类编号错误！";
            exit(json_encode($response));
        }

        //判断文章标题
        if(empty($question_content)){
            $response['code'] = "99";
            $response['message'] = "提问内容不能为空！";
            exit(json_encode($response));
        }else{
            $question_content = base64_decode($question_content);
            $question_content = ltrim($question_content,"\"");
            $question_content = rtrim($question_content,"\"");
        }

        //判断文章标题
        if(empty($reply_content)){
            $response['code'] = "99";
            $response['message'] = "回答内容不能为空！";
            exit(json_encode($response));
        }else{
            $reply_content = base64_decode($reply_content);
            $reply_content = ltrim($reply_content,"\"");
            $reply_content = rtrim($reply_content,"\"");
        }

        //判断文章是否已经存在
        $where['source_url'] = $source_url;
        $question_count = IDb::getInstance("question")->setDbWhere($where)->con();
        if($question_count <= 0) {
            //整理数据
            $question_data['question_type'] = $question_type;
            $question_data['question_content'] = $question_content;
            $question_data['reply_content'] = $reply_content;
            $question_data['source_url'] = $source_url;
            $question_data['create_time'] = get_date_time();
            $question_data['reply_time'] = get_date_time();
            $question_add = IDb::getInstance("question")->setDbData($question_data)->add();
            if ($question_add === false) {
                $response['code'] = "99";
                $response['message'] = "添加文章资讯失败！";
                exit(json_encode($response));
            }
        }else{
            $response['code'] = "99";
            $response['message'] = "重复数据！";
            exit(json_encode($response));
        }

        //添加成功
        $response['code'] = "0";
        $response['message'] = "添加成功！";
        exit(json_encode($response));
    }
}